using UnityEngine;
using System.Threading;

public class Sound
{
	const int INTERVAL = 5;
	const int MAXTIME = 100;
	public static int status, postem, timestart;
	static string filenametemp;
	static float volumetem;
	public static bool isSound = true;
	public static AudioSource SoundWater,SoundRun,SoundBGLoop;
	public static void stop ()
	{
		for (int i = 0; i < player.Length; i++) {
			if (player [i] != null)
				player [i].audio.Pause ();
		}
	}

	public static AudioClip[] mysound;
	public static GameObject[] player;
	public static string[] fileName = { "0", "1", "2", "3", "4", "5", "6"
										,"7", "8", "9", "10", "11", "12" 
										, "13", "14", "15", "16", "17", "18"
										, "19", "29", "21", "22", "23", "24"
										, "25","26","27","28","29","30","31","32","33" };
		public static sbyte MLogin 			= 0;
		public static sbyte MBClick			= 1;
		public static sbyte MTone 			= 2;
		public static sbyte MSanzu 			= 3;
		public static sbyte MChakumi		= 4;
		public static sbyte MChai 			= 5;
		public static sbyte MOshin 			= 6;
		public static sbyte MEchigo 		= 7;
		public static sbyte MKojin 			= 8;
		public static sbyte MHaruna 		= 9;
		public static sbyte MHirosaki		= 10;
		public static sbyte MOokaza			= 11;
		public static sbyte MGiotuyet		= 12;
		public static sbyte MHangdong		= 13;
		public static sbyte MDeKeu			= 14;
		public static sbyte MChimKeu		= 15;
		public static sbyte MBuocChan		= 16;
		public static sbyte MNuocChay 		= 17;
		public static sbyte MBomMau			= 18;
		public static sbyte MKiemGo 		= 19;
		public static sbyte MKiem 			= 20;
		public static sbyte MTieu 			= 21;
		public static sbyte MKunai 			= 22;
		public static sbyte MCung 			= 23;
		public static sbyte MDao 			= 24;
		public static sbyte MQuat 			= 25;
		public static sbyte MCung2 			= 26;
		public static sbyte MTieu2 			= 27;
		public static sbyte MTieu3 			= 28;
		public static sbyte MKiem2 			= 29;
		public static sbyte MKiem3 			= 30;
		public static sbyte MDao2 			= 31;
		public static sbyte MDao3 			= 32;
		public static sbyte MCung3 			= 33;

		public static bool bMuzikDisable;

		public static void init ()
		{
		GameObject AudioObjWater = new GameObject ();
		AudioObjWater.name = "Audio Player Water";
		AudioObjWater.transform.position = Vector3.zero;
		AudioObjWater.AddComponent<AudioListener> ();
		SoundWater = AudioObjWater.AddComponent<AudioSource> ();
		
		GameObject AudioObj = new GameObject ();
		AudioObj.name = "Audio Player";
		AudioObj.transform.position = Vector3.zero;
		AudioObj.AddComponent<AudioListener> ();
		SoundBGLoop = AudioObj.AddComponent<AudioSource> ();
			
			
		GameObject AudioObjRun = new GameObject ();
		AudioObjRun.name = "Audio Player Run";
		AudioObjRun.transform.position = Vector3.zero;
		AudioObjRun.AddComponent<AudioListener> ();
		SoundRun=AudioObjRun.AddComponent<AudioSource> ();
		
		player = new GameObject[34];
		mysound = new AudioClip[34];


		for(int i=0;i<player.Length;i++)
			getAssetSoundFile(fileName[i],i);
		bMuzikDisable = false;
	}

	public static void getAssetSoundFile (string fileName, int type)
	{

		stop (type);
		string t = "";
		t = Main.res + "/music/" + fileName;      
		load (t, type);
	}

	public static void stopAll ()
	{
		bMuzikDisable = true;
		for (int i = 0; i < mysound.Length; i++) {
			Sound.stop (i);
		}
	}

	public static void stopAllBg ()
	{
		//bMuzikDisable = true;
		for (int i = 0; i < mysound.Length; i++) {
			Sound.stop (i);
		}
		sTopSoundBG(0);
		sTopSoundRun();
		stopSoundNatural(0);
	}

	public static void update ()
	{
       
	}

	public static void stopMusic (int x)
	{
		if (!bMuzikDisable)
			stop (x);
	}

	public static void play (int id, float volume)
	{
       
		if (!bMuzikDisable)
			start (volume, id);
       
	}

	public static void playSoundRun (int id, float volume)
	{
		if (!bMuzikDisable) {
			if (SoundRun == null)
				return; 
			SoundRun.audio.loop = true;
			SoundRun.audio.clip = mysound [id];
			SoundRun.audio.volume = volume;
			SoundRun.audio.Play ();
//        Main.AudioPlayer.audio.PlayOneShot(mysound[id],volume);
		}
	}
	
	public static void sTopSoundRun ()
	{
		SoundRun.audio.Stop ();
	}

	public static bool isPlayingSound ()
	{
		if (SoundRun == null)
			return false;         
		bool isPlay = SoundRun.audio.isPlaying;
		return isPlay;
	}	
	
	//------------------------------------------------
	//--moi them 
	public static void playSoundNatural(int id,float volume,bool isLoop){
		if (!bMuzikDisable) {
			if (SoundBGLoop == null)
				return; 
			SoundWater.audio.loop = isLoop;
			SoundWater.audio.clip = mysound [id];
			SoundWater.audio.volume = volume;
			SoundWater.audio.Play ();
		}
	}
	public static void stopSoundNatural(int id){
		SoundWater.audio.Stop();
	}
	public static bool isPlayingSoundatural (int id)
	{
		if (SoundWater == null)
			return false;         
		bool isPlay = SoundWater.audio.isPlaying;
		return isPlay;
	}
	//--moi them
	public static void playSoundBGLoop(int id, float volume)
	{
		if (!bMuzikDisable) {
			if (SoundBGLoop == null)
				return; 
			SoundBGLoop.audio.loop = true;
			SoundBGLoop.audio.clip = mysound [id];
			SoundBGLoop.audio.volume = volume;
			SoundBGLoop.audio.Play ();
//        Main.AudioPlayer.audio.PlayOneShot(mysound[id],volume);
		}
	}
	
	public static void sTopSoundBG (int id)
	{
		SoundBGLoop.audio.Stop ();
	}

	public static bool isPlayingSoundBG (int id)
	{
		if (SoundBGLoop == null)
			return false;         
		bool isPlay = SoundBGLoop.audio.isPlaying;
		return isPlay;
	}
	//-----------
	public static void load (string filename, int pos)
	{      
		if (Thread.CurrentThread.Name == Main.mainThreadName)
			__load (filename, pos);
		else
			_load (filename, pos);
	}

	private static void _load (string filename, int pos)
	{
		if (status != 0) {
			Cout.LogError ("CANNOT LOAD AUDIO " + filename + " WHEN LOADING " + filenametemp);
			return;
		}
		filenametemp = filename;
		postem = pos;
		status = 2;
		int i = 0;
		while (i<MAXTIME) {
			Thread.Sleep (INTERVAL);
			if (status == 0) {              
				break; // load done
			}
			i++;
		}
		if (i == MAXTIME)
			Cout.LogError ("TOO LONG FOR LOAD AUDIO " + filename);
		else
			Cout.Log ("Load Audio " + filename + " done in " + (i * INTERVAL) + "ms");		
	}

	private static void __load (string filename, int pos)
	{
		mysound [pos] = (AudioClip)Resources.Load (filename, typeof(AudioClip));
		GameObject.Find ("Main Camera").AddComponent ("AudioSource");
		player [pos] = GameObject.Find ("Main Camera");      
       
	}

	public static void start (float volume, int pos)
	{
		
		if (Thread.CurrentThread.Name == Main.mainThreadName)
			__start (volume, pos);
		else
			_start (volume, pos);
	}

	public static void _start (float volume, int pos)
	{
		if (status != 0) {
			Debug.LogError ("CANNOT START AUDIO WHEN STARTING");
			return;
		}
		volumetem = volume;
		postem = pos;
		status = 3;
		int i = 0;
		while (i<MAXTIME) {
			Thread.Sleep (INTERVAL);
			if (status == 0)
				break; // start done
			i++;
		}
		if (i == MAXTIME)
			Debug.LogError ("TOO LONG FOR START AUDIO");
		else
			Debug.Log ("Start Audio done in " + (i * INTERVAL) + "ms");
	}
	
	public static void __start (float volume, int pos)
	{
		if (player [pos] == null)
			return;         
		player [pos].audio.PlayOneShot (mysound [pos], volume);
       
     
	}	
	//====================
	public static void stop (int pos)
	{
		if (Thread.CurrentThread.Name == Main.mainThreadName)
			__stop (pos);
		else
			_stop (pos);
	}

	public static void _stop (int pos)
	{
		if (status != 0) {
			Debug.LogError ("CANNOT STOP AUDIO WHEN STOPPING");
			return;
		}
		postem = pos;
		status = 4;
		int i = 0;
		while (i<MAXTIME) {
			Thread.Sleep (INTERVAL);
			if (status == 0)
				break; // start done
			i++;
		}
		if (i == MAXTIME)
			Debug.LogError ("TOO LONG FOR STOP AUDIO");
		else
			Debug.Log ("Stop Audio done in " + (i * INTERVAL) + "ms");
	}
	
	public static void __stop (int pos)
	{
		if (player [pos] != null)
			player [pos].audio.Stop ();
	}
	
}


